package com.galeno.day04;

import com.galeno.FlinkUtils;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;

/**
 * @author galeno
 * @Title:
 * @Description:
 * @date 2021/10/2010:54
 */
public class CountWindowDemo2 {
    public static void main(String[] args) throws Exception {
        Configuration configuration = new Configuration();
        configuration.setInteger("rest.port",8081);
        StreamExecutionEnvironment locEnvWebUI = FlinkUtils.getLocEnvWebUI(configuration);
        DataStreamSource<String> lines = locEnvWebUI.socketTextStream("192.168.77.101", 8888);
        SingleOutputStreamOperator<Tuple2<String, String>> wc = lines.map(x -> {
            String[] split = x.split(",");
            String word = split[0];
            String count = split[1];
            return Tuple2.of(word, count);
        }).returns(Types.TUPLE(Types.STRING, Types.INT));
        KeyedStream<Tuple2<String, String>, String> ks = wc.keyBy(x -> x.f0);
        WindowedStream<Tuple2<String, String>, String, GlobalWindow> ws = ks.countWindow(5);
        SingleOutputStreamOperator<Tuple2<String, String>> sum = ws.sum(1);
        sum.print();
        locEnvWebUI.execute();


    }
}
